package services

import (
	"fmt"
	"longmen/server/config/global"
	"longmen/server/pkg/common/helper/util"
	"longmen/server/pkg/common/sdk/tx"
	"longmen/server/pkg/db/mysql/db_main"
	"longmen/server/pkg/db/mysql/models/models_live"
	"longmen/server/pkg/db/mysql/models/models_main"
	"longmen/server/pkg/db/redis/cache"

	"longmen/server/pkg/common/api_models"
)

// GetAds 获取广告位图片
func (s *Service) GetAds(equipment uint8, category uint8) ([]*api_models.AdsResp, error) {
	var (
		err      error
		fName    = "GetAds"
		adsQuery []*models_main.Ads
		resp     []*api_models.AdsResp
	)

	// err = global.GetR().Model(&models_main.Ads{}).Where("equipment=? and end_time>? and status=1", equipment, time.Now().Unix()).Find(&adsQuery).Error
	// if err != nil {
	// 	return nil, fmt.Errorf("%s %s", util.Convert(fName), err.Error())
	// }

	if adsQuery, err = cache.GetAdsInfo(); err != nil {
		return nil, fmt.Errorf("%s %s", util.Convert(fName), err.Error())
	}

	var matchIDs []int

	for _, item := range adsQuery {
		if item.Equipment != int(equipment) || item.Type != int(category) {
			continue
		}
		if item.MatchID > 0 {
			matchIDs = append(matchIDs, item.MatchID)
		}
		resp = append(resp, &api_models.AdsResp{
			Title:       item.Title,
			ImageUrl:    item.ImageUrl,
			SEMImageURL: item.SEMImageURL,
			JumpUrl:     item.JumpUrl,
			JumpType:    item.JumpType,
			Type:        item.Type,
			StoreID:     item.StoreID,
			StartTime:   item.StartTime,
			EndTime:     item.EndTime,
			MatchID:     item.MatchID,
			SportID:     item.SportID,
		})
	}

	if len(matchIDs) > 0 {
		var lives []models_live.Live
		err = global.Live().Model(&models_live.Live{}).Where("status=1 and matchid in (?)", matchIDs).Group("matchid").Order("RAND()").Find(&lives).Error
		if err != nil {
			return nil, fmt.Errorf("%s %s", util.Convert(fName), err.Error())
		}

		for idx, v := range resp {
			for _, l := range lives {
				if v.MatchID == l.Matchid {
					resp[idx].AnchorID = l.Anchorid
					resp[idx].PullUrl = l.PullFlvUrl
					if l.PlayFromSource != 1 {
						resp[idx].PullUrl = tx.CreateLiveUrl(tx.HostFLV, l.Stream, 0)[0]
						resp[idx].PullFlvUrl = tx.CreateLiveUrl(tx.HostFLV, l.Stream, 0)[0]
						resp[idx].PullRtmpUrl = tx.CreateLiveUrl(tx.HostRTMP, l.Stream, 0)[0]
						resp[idx].PullM3u8Url = tx.CreateLiveUrl(tx.HostHLS, l.Stream, 0)[0]
					} else {
						resp[idx].PullFlvUrl2 = l.PullUrl
						resp[idx].PullRtmpUrl = l.PullUrl
						resp[idx].PullM3u8Url = l.PullUrl
					}

					resp[idx].PlayFromSouce = int(l.PlayFromSource)
					resp[idx].RelayVideo = int(l.ReplyVideo)
					resp[idx].WatermarkPosition = l.WatermarkPosition
					resp[idx].SportID = l.Category
				}
			}
		}
	}

	return resp, nil
}

func (s *Service) GetLaunchAd() (*models_main.Ads, error) {
	fName := "GetLaunchAd"
	ads, err := db_main.GetAdsParam(map[string]interface{}{"status": 1, "type": 1}, "create_time asc")
	if err != nil {
		return nil, fmt.Errorf("%s%s", util.Convert(fName), err.Error())
	}
	return ads, nil
}

func (s *Service) GetHomeScrollAd() ([]*models_main.Ads, error) {
	fName := "GetHomeScrollAd"
	list, err := db_main.GetAdsListParam(map[string]interface{}{"status": 1, "type": 2}, "create_time asc", 0, 0)
	if err != nil {
		return nil, fmt.Errorf("%s%s", util.Convert(fName), err.Error())
	}
	return list, nil
}

func (s *Service) GetHomePopAd() ([]*models_main.Ads, error) {
	fName := "GetHomePopAd"
	list, err := db_main.GetAdsListParam(map[string]interface{}{"status": 1, "type": 3}, "create_time asc", 0, 0)
	if err != nil {
		return nil, fmt.Errorf("%s%s", util.Convert(fName), err.Error())
	}
	return list, nil
}

func (s *Service) GetNavBg() (*models_main.Ads, error) {
	fName := "GetNavBg"
	ads, err := db_main.GetAdsParam(map[string]interface{}{"status": 1, "type": 6}, "create_time desc")
	if err != nil {
		return nil, fmt.Errorf("%s%s", util.Convert(fName), err.Error())
	}
	return ads, nil
}

func (s *Service) GetHomeBannerBg() (*models_main.Ads, error) {
	return cache.GetHomeBannerBg()
}
